<?

require_once SITE_ROOT_PATH."/_lib/classes/class.tree.php";

class destinations extends tree {



// FUNCTION // get single tree item
function get_destinations($params){
	
	$params = in($params);
	


	if(!empty($params)){
		
		if(!empty($params['type_id'])) $sql_where .= " AND type_id = '".$params['type_id']."' ";
		if(!empty($params['parent_id'])) $sql_where .= " AND SUBSTRING_INDEX(lineage_base, ',', -1) = '".$params['parent_id']."' ";
		if(!empty($params['relation_id'])) $sql_where .= " AND FIND_IN_SET('".$params['relation_id']."', CONCAT(lineage_base,',',tree_id)) ";
		if(!empty($params['prefix'])) $sql_where .= " AND UPPER(LEFT(name, 1)) = '".$params['prefix']."' ";
		if(!empty($params['name'])) $sql_where .= " AND name LIKE '%".$params['name']."%' ";
	
		$sql = "SELECT LENGTH( lineage_base ) - LENGTH( REPLACE( lineage_base,  ',',  '' ) ) as depth, SUBSTRING_INDEX(lineage_base, ',', -1) AS parent_id, CONCAT(lineage_base,',',tree_id) AS lineage, ".implode(',', $this->switch_table_fields())."
		FROM tree
		WHERE 1 = 1
		".$sql_where."
		ORDER BY name";

		return apply_query($sql);
		
	}

}



// FUNCTION // validate update form
function validate_details_form($form_data){
	
	$my_array = array();
	
	if(!$form_data['name']) array_push($my_array, 'Please enter the destination name.');
	if(!$form_data['description']) array_push($my_array, 'Please enter the description.');


	return $my_array;
	
}



// FUNCTION // search for a destination based on the airport code
function destination_lookup($params){


	$params = in($params);

	if($params['airport_code']) $sql_where .= " AND FIND_IN_SET('".$params['airport_code']."',airport_codes_related_list) ";
	if($params['type_id']) $sql_where .= " AND type_id = '".$params['type_id']."' ";
	if($params['position']){
		switch($params['position']){
			case "top";
				$sql_order .= " ORDER BY (LENGTH( lineage_base ) - LENGTH( REPLACE( lineage_base,  ',',  '' ) )) ASC ";
			break;
			case "bottom";
				$sql_order .= " ORDER BY (LENGTH( lineage_base ) - LENGTH( REPLACE( lineage_base,  ',',  '' ) )) DESC ";
			break;
		}
	}
	
	$sql = "SELECT name, description, airport_code_search, airport_codes_search_list, airport_codes_related_list, map_image_url, tree_id, yahoo_weather_location_id, longtitude, latitude , show_google_map, type_id, SUBSTRING_INDEX(lineage_base, ',', -1) AS parent_id, lineage_base, LENGTH( lineage_base ) - LENGTH( REPLACE( lineage_base,  ',',  '' ) ) AS depth,  CONCAT(lineage_base,',',tree_id) AS lineage
	FROM tree
	WHERE 1 = 1
	".$sql_where."
	".$sql_order."
	LIMIT 1 ";

	return apply_query($sql, $num_rows, $params['cache']);

}




// FUNCTION // get a random destinations
function get_random_destination(){

	
	$sql = "SELECT name, description, tree_id, main_photo 
	FROM tree
	WHERE type_id = '1'
	ORDER BY RAND()
	LIMIT 1 ";

		return apply_query($sql);

}

// Return Ski destination names for summer time
function ski_destination_name_filter($destination, $date_depart){
	
	$month = round(substr($date_depart, 2, 2));

	if($month > 4 or $month < 11) $destination = str_replace('(Ski)', '', $destination);
	
	return $destination;
	
}


}

?>